package com.mz.mattress.admin.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mz.mattress.admin.api.req.CouponQueryReq;
import com.mz.mattress.admin.mapper.CouponGoodsMapper;
import com.mz.mattress.admin.mapper.CouponMapper;
import com.mz.mattress.admin.mapper.CouponSendRecordMapper;
import com.mz.mattress.admin.service.CouponService;
import com.mz.mattress.api.common.Result;
import com.mz.mattress.api.entity.Coupon;
import com.mz.mattress.api.entity.CouponGoods;
import com.mz.mattress.api.entity.CouponSendRecord;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

@Service
@Slf4j
public class CouponServiceImpl implements CouponService {

    @Autowired
    private CouponMapper couponMapper;

    @Autowired
    private CouponGoodsMapper couponGoodsMapper;

    @Autowired
    private CouponSendRecordMapper couponSendRecordMapper;

    @Override
    public Result<IPage<Coupon>> listPage(CouponQueryReq req) {
        Page page = req.newPage();
        //查询一级服务商
        Page<Coupon> list = couponMapper.listPage(page, req.getCity(), req.getStatus(), req.getType(), req.getStartTime(), req.getEndTime());
        return Result.ok(list);
    }

    @Override
    public Result<Coupon> getById(Integer id) {

        Coupon coupon = couponMapper.selectById(id);

        List<Integer> goodsIds = couponGoodsMapper.selectByCouponId(id);
        coupon.setGoodsIds(goodsIds);

        return Result.ok(coupon);
    }

    @Override
    public Result add(Coupon coupon) {

        if (StringUtils.isEmpty(coupon.getName())) {
            return Result.fail("请输入名称");
        }

        couponMapper.insert(coupon);

        addCouponGoods(coupon);

        return Result.ok();
    }

    @Override
    public Result updateById(Coupon coupon) {

        couponMapper.updateById(coupon);

        couponGoodsMapper.deleteByCouponId(coupon.getId());
        addCouponGoods(coupon);

        return Result.ok();
    }

    private void addCouponGoods(Coupon coupon) {
        List<Integer> goodsIds = coupon.getGoodsIds();
        if (goodsIds != null && goodsIds.size() > 0) {
            for (Integer goodsId : goodsIds) {
                CouponGoods goods = new CouponGoods();
                goods.setGoodsId(goodsId);
                goods.setCouponId(coupon.getId());
                couponGoodsMapper.insert(goods);
            }
        }
    }

    @Override
    public Result deleteByIds(Set<Integer> ids) {
        couponMapper.deleteBatchIds(ids);
        return Result.ok();
    }

    @Override
    public Result sendCoupon(CouponSendRecord record) {

        record.setStatus(0);
        couponSendRecordMapper.insert(record);

        //会员优惠券列表 自动刷新，加载发放的优惠券


//        //远程调用
//        String url = SEND_COUPON_URL + record.getId();
//        log.info("url=" + url);
//        String resp = HttpUtil.get(url);
//        log.info("url_resp=" + resp);

        return Result.ok();
    }

}
